package br.unirio.brasileirao.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CriptografiaUtils {

	public static String criptografarComSha256(String value) 
	{
		if(value == null) 
			return null;
		
		try 
		{
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update(value.getBytes());
			return byteArrayToString(md.digest());
		} 
		catch (NoSuchAlgorithmException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	private static String byteArrayToString(byte[] encrypted) 
	{
		String hex = "0123456789ABCDEF";
		String result = "";

		for (int i = 0; i < encrypted.length; i++) 
		{
			int value = encrypted[i];
			if (value < 0)
				value = 256 + value;
			result += hex.charAt(value / 16);
			result += hex.charAt(value % 16);
		}
		return result;
	}
}
